import { isPcStore } from '@/stores';
import { useSetAtom } from 'jotai';
import { useState, useEffect } from 'react';

// 自定义Hook: useWindowWidth
export default function useWindowWidth() {
  const [width, setWidth] = useState(0);
  const setIsPc = useSetAtom(isPcStore);

  useEffect(() => {
    function handleResize() {
      const width = window.innerWidth;
      setIsPc(width > 1100);
      setWidth(width);
    }

    if (typeof window !== 'undefined') {
      window.addEventListener('resize', handleResize);
      handleResize(); // 初始化宽度
      return () => window.removeEventListener('resize', handleResize);
    }
  }, []);

  return width;
}
